home *** CD-ROM | disk | FTP | other *** search
/ Delphi Developer's Kit 1996 / Delphi Developer's Kit 1996.iso / power / extfile / extfile.pas < prev    next >
Pascal/Delphi Source File  |  1995-12-22  |  6KB  |  209 lines

  1. {-------------------------------------------------------------------}
  2. { EXTFILE - Extended FileListbox: shows size, date & time of files  }
  3. { v. 1.00 July, 21 1995                                             }
  4. {-------------------------------------------------------------------}
  5. { Copyright Enrico Lodolo                                           }
  6. { via F.Bolognese 27/3 - 440129 Bologna - Italy                     }
  7. { CIS 100275,1255 - Internet ldlc18k1@bo.nettuno.it                 }
  8. {-------------------------------------------------------------------}
  9.  
  10. unit ExtFile;
  11.  
  12. interface
  13.  
  14. uses
  15.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  16.   Forms, Dialogs, StdCtrls, FileCtrl;
  17.  
  18. type
  19.   TExtFileListBox = class(TFileListBox)
  20.   private
  21.     FShowSize:Boolean;
  22.     FShowDate:Boolean;
  23.     FShowTime:Boolean;
  24.     FSizePos:Integer;
  25.     FDatePos:Integer;
  26.     FTimePos:Integer;
  27.     FNameWd:Integer;
  28.     FSizeWd:Integer;
  29.     FDateWd:Integer;
  30.     FTimeWd:Integer;
  31.     procedure SetShowSize(Value:Boolean);
  32.     procedure SetShowDate(Value:Boolean);
  33.     procedure SetShowTime(Value:Boolean);
  34.     procedure SetSizePos(Value:Integer);
  35.     procedure SetDatePos(Value:Integer);
  36.     procedure SetTimePos(Value:Integer);
  37.   protected
  38.     procedure SetWidths;
  39.     procedure DrawItem(Index:Integer;Rect:TRect;State:TOwnerDrawState); override;
  40.   public
  41.     constructor Create(AOwner:TComponent); override;
  42.   published
  43.     property ShowSize:Boolean read FShowSize write SetShowSize default True;
  44.     property ShowDate:Boolean read FShowDate write SetShowDate default True;
  45.     property ShowTime:Boolean read FShowTime write SetShowTime default True;
  46.     property SizePos:Integer read FSizePos write SetSizePos default -1;
  47.     property DatePos:Integer read FDatePos write SetDatePos default -1;
  48.     property TimePos:Integer read FTimePos write SetTimePos default -1;
  49.   end;
  50.  
  51. procedure Register;
  52.  
  53. implementation
  54.  
  55. procedure TExtFileListBox.SetShowSize(Value:Boolean);
  56.  
  57. begin
  58.      if Value<>FShowSize then
  59.        begin
  60.          FShowSize:=Value;
  61.          Update;
  62.        end;
  63. end;
  64.  
  65. procedure TExtFileListBox.SetShowDate(Value:Boolean);
  66.  
  67. begin
  68.      if Value<>FShowDate then
  69.        begin
  70.          FShowDate:=Value;
  71.          Update;
  72.        end;
  73. end;
  74. procedure TExtFileListBox.SetShowTime(Value:Boolean);
  75.  
  76. begin
  77.      if Value<>FShowTime then
  78.        begin
  79.          FShowTime:=Value;
  80.          Update;
  81.        end;
  82. end;
  83.  
  84. procedure TExtFileListBox.SetSizePos(Value:Integer);
  85.  
  86. begin
  87.      if Value<>FSizePos then
  88.        begin
  89.          FSizePos:=Value;
  90.          Update;
  91.        end;
  92. end;
  93.  
  94. procedure TExtFileListBox.SetDatePos(Value:Integer);
  95.  
  96. begin
  97.      if Value<>FDatePos then
  98.        begin
  99.          FDatePos:=Value;
  100.          Update;
  101.        end;
  102. end;
  103.  
  104. procedure TExtFileListBox.SetTimePos(Value:Integer);
  105.  
  106. begin
  107.      if Value<>FTimePos then
  108.        begin
  109.          FTimePos:=Value;
  110.          Update;
  111.        end;
  112. end;
  113.  
  114. constructor TExtFileListBox.Create(AOwner:TComponent);
  115.  
  116. {-- Creates the component and sets the defaults }
  117.  
  118. begin
  119.      inherited Create(AOwner);
  120.      FShowSize:=True;
  121.      FShowDate:=True;
  122.      FShowTime:=True;
  123.      FSizePos:=-1;
  124.      FDatePos:=-1;
  125.      FTimePos:=-1;
  126. end;
  127.  
  128. procedure TExtFileListBox.SetWidths;
  129.  
  130. {-- Calculates the max. widths of name, size, date and time }
  131.  
  132. begin
  133.      with Canvas do
  134.        begin
  135.          FNameWd:=TextWidth('aaaaaaaa.mmm');
  136.          FSizeWd:=TextWidth('888888888');
  137.          FDateWd:=TextWidth('888/88/88');
  138.          FTimeWd:=TextWidth('888.88.88');
  139.        end;
  140. end;
  141.  
  142. procedure TExtFileListBox.DrawItem(Index:Integer;Rect:TRect;
  143.                                    State: TOwnerDrawState);
  144.  
  145. {-- Draws a line of the ListBox }
  146.  
  147. var Bitmap: TBitmap;
  148.     Offset: Integer;
  149.     OldAlign: Word;
  150.     SR: TSearchRec;
  151.     DT: TDateTime;
  152.     St:String;
  153.  
  154. begin
  155.      inherited DrawItem(Index,Rect,State);
  156.      with Canvas do
  157.        begin
  158.          SetWidths;
  159.          Offset:=Rect.Left+FNameWd;
  160.          if (FShowGlyphs=True) and (Bitmap<>nil) then
  161.            begin
  162.              Bitmap:=TBitmap(Items.Objects[Index]);
  163.              Offset:=Offset+Bitmap.Width+6;
  164.            end;
  165.          {-- Retrieves Size, Date and Time of the current file }
  166.          if Directory[Length(Directory)]='\' then
  167.            St:=Directory+Items[Index]
  168.          else St:=Directory+'\'+Items[Index];
  169.          FindFirst(St,faAnyFile,SR);
  170.          DT:=FileDateToDateTime(SR.Time);
  171.          {-- Right alignes the text }
  172.          OldAlign:=SetTextAlign(Handle,ta_right);
  173.          if FShowSize then
  174.            begin
  175.              if FSizePos=-1 then Offset:=Offset+FSizeWd
  176.              else Offset:=FSizePos;
  177.              TextOut(Offset,Rect.Top,IntToStr(SR.Size));
  178.            end;
  179.          if FShowDate then
  180.            begin
  181.              if FDatePos=-1 then Offset:=Offset+FDateWd
  182.              else Offset:=FDatePos;
  183.              try
  184.                TextOut(Offset,Rect.Top,DateToStr(DT));
  185.              finally
  186.              end;
  187.            end;
  188.          if FShowTime then
  189.            begin
  190.              if FTimePos=-1 then Offset:=Offset+FTimeWd
  191.              else Offset:=FTimePos;
  192.              try
  193.              TextOut(Offset,Rect.Top,TimeToStr(DT));
  194.              finally
  195.              end;
  196.            end;
  197.          {-- Sets the text alignment to the original value }
  198.          SetTextAlign(Handle,OldAlign);
  199.        end;
  200. end;
  201.  
  202. procedure Register;
  203.  
  204. begin
  205.      RegisterComponents('Samples', [TExtFileListBox]);
  206. end;
  207.  
  208. end.
  209.